package com.example.dryclean_admin.common;

import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;

import java.util.UUID;

public class PasswordUtils {
    public static String encrypt(String password){
        String salt = UUID.randomUUID().toString().replace("-","");
        String saltPassword = DigestUtils.md5DigestAsHex((salt + password).getBytes());
        String finalPassword = salt + "$" + saltPassword;
        return finalPassword;
    }
    public static String encrypt(String password,String salt){
        String saltPassword = DigestUtils.md5DigestAsHex((salt + password).getBytes());
        String finalPassword = salt + "$" + saltPassword;
        return finalPassword;
    }
    public static boolean check(String inputPassword,String finalPassword){
        if(StringUtils.hasLength(inputPassword) && StringUtils.hasLength(finalPassword)
                && finalPassword.length() == 65){
            String salt = finalPassword.split("\\$")[0];
            String confirmPassword = PasswordUtils.encrypt(inputPassword,salt);
            return confirmPassword.equals(finalPassword);
        }
        return false;
    }
}
